<template>
  <div class="global-padding">
    <div style="margin-bottom: 10px;">
      <el-input v-model="searchValue" placeholder="请输入内容" style="width: 200px;margin-right: 10px;" />
      <el-date-picker
        v-model="datePickerValue"
        type="daterange"
        align="right"
        unlink-panels
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="margin-right: 10px;"
        @change="datePickerConfirm"
      />
      <el-select v-model="optionValue" placeholder="请选择" @change="selectOptionChange">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" style="margin-left: 10px;" @click="conditionSearch">条件搜索</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="articleId"
        label="ID"
      />
      <el-table-column
        fixed
        prop="createTime"
        label="日期"
      />
      <el-table-column
        prop="articleTitle"
        label="通知标题"
      />
      <el-table-column
        prop="priority"
        label="优先级"
      />
      <el-table-column
        label="面向群体"
      >
        <template slot-scope="scope">
          <!-- 订单状态 0等待接单 1待取货 2待收货 3订单完成 4用户已评价 -->
          <div v-if="scope.row.type == 0">全体成员</div>
          <div v-if="scope.row.type == 1">用户</div>
          <div v-if="scope.row.type == 2">司机</div>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="confirmDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="编辑" :visible.sync="updateDialogFormVisible">
      <el-form>
        <el-form-item label="文章标题">
          <el-input v-model="curRow.articleTitle" autocomplete="off" />
        </el-form-item>
        <el-form-item label="文章内容">
          <tinymce v-model="curRow.articleHtml" :height="300" />
        </el-form-item>
        <el-form-item label="优先级">
          <el-input v-model="curRow.priority" autocomplete="off" />
        </el-form-item>
        <el-select v-model="curRow.type" placeholder="面向群体" @change="selectOptionChange">
          <el-option
            label="全体"
            value="0"
          />
          <el-option
            label="用户"
            value="1"
          />
          <el-option
            label="司机"
            value="2"
          />
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmUpdate">确 定</el-button>
      </div>
    </el-dialog>
    <div style="margin-top: 10px;text-align: center;">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="curPage"
        :page-size="pageSize"
        :total="total"
        @current-change="curPageChange"
      />
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'
import Tinymce from '@/components/Tinymce'
export default {
  components: { Tinymce },
  data() {
    return {
      optionValue: '',
      curPage: 1,
      pageSize: 8,
      total: 1,
      tableData: [],
      dateField: 'create_time',
      datePickerValue: '',
      optionField: 'type',
      options: [{
        value: '',
        label: '全部'
      }, {
        value: '0',
        label: '全体'
      }, {
        value: '1',
        label: '用户'
      }, {
        value: '2',
        label: '司机'
      }],
      searchField: 'article_title',
      searchValue: '',
      curRow: {},
      updateDialogFormVisible: false
    }
  },
  mounted() {
    this.reqListData()
  },
  methods: {
    handleUpdate(row) {
      console.log(row)
      this.curRow = row
      this.updateDialogFormVisible = true
    },
    curPageChange(v) {
      console.log(v)
      this.curPage = v
      this.reqListData()
    },
    datePickerConfirm(v) {
      console.log(v)
      console.log(v[0])
    },
    selectOptionChange(v) {
      console.log(v)
    },
    reqListData() {
      request.get('article/articleList', { params: {
        curPage: this.curPage,
        pageSize: this.pageSize,
        dateField: this.dateField,
        startDate: this.datePickerValue[0],
        endDate: this.datePickerValue[1],
        searchField: this.searchField,
        search: this.searchValue,
        statusField: this.optionField,
        statusValue: this.optionValue
      }}).then(res => {
        this.tableData = []
        console.log(res)
        for (const v of res.records) {
          this.tableData.push(v)
        }
        this.curPage = res.current
        this.pageSize = res.size
        this.total = res.total
      })
    },
    conditionSearch() {
      this.reqListData()
    },
    confirmDelete(row) {
      this.$alert('确定删除吗', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          if (action === 'confirm') {
            request.delete(`/article/articleDelete/${row.articleId}`).then(res => {
              this.$message({
                type: 'info',
                message: `删除成功`
              })
              this.reqListData()
            })
          }
        }
      })
    },
    handleConfirmUpdate() {
      this.updateDialogFormVisible = false
      request.put(`/article/articleUpdate`, {
        'articleHtml': this.curRow.articleHtml,
        'articleId': this.curRow.articleId,
        'articleTitle': this.curRow.articleTitle,
        'priority': this.curRow.priority,
        'type': this.curRow.type
      }).then(res => {
        this.$message({
          type: 'info',
          message: `更新成功`
        })
        this.reqListData()
      })
    }
  }
}
</script>
